home *** CD-ROM | disk | FTP | other *** search
/ Chip 1996 September / CHIP Eylül 1996.iso / utils / povray / povsrc.lzh / machine / amiga / amiga.c next >
C/C++ Source or Header  |  1994-02-06  |  26KB  |  993 lines

  1. /****************************************************************************
  2. *                   amiga.c
  3. *
  4. *  This module handles all of the Amiga-specific code for the raytracer.
  5. *
  6. *  from Persistence of Vision Raytracer
  7. *  Copyright 1993 Persistence of Vision Team
  8. *---------------------------------------------------------------------------
  9. *  NOTICE: This source code file is provided so that users may experiment
  10. *  with enhancements to POV-Ray and to port the software to platforms other 
  11. *  than those supported by the POV-Ray Team.  There are strict rules under
  12. *  which you are permitted to use this file.  The rules are in the file
  13. *  named POVLEGAL.DOC which should be distributed with this file. If 
  14. *  POVLEGAL.DOC is not available or for more info please contact the POV-Ray
  15. *  Team Coordinator by leaving a message in CompuServe's Graphics Developer's
  16. *  Forum.  The latest version of POV-Ray may be found there as well.
  17. *
  18. * This program is based on the popular DKB raytracer version 2.12.
  19. * DKBTrace was originally written by David K. Buck.
  20. * DKBTrace Ver 2.0-2.12 were written by David K. Buck & Aaron A. Collins.
  21. *
  22. * Modifications by Dave Park.
  23. *
  24. *****************************************************************************/
  25.  
  26.  
  27. #include "frame.h"
  28. #include "povproto.h"
  29.  
  30. #include <proto/exec.h>
  31. #include <proto/intuition.h>
  32. #include <proto/graphics.h>
  33. #include <proto/dos.h>
  34. #include <proto/expansion.h>
  35. #include <exec/types.h>
  36. #include <intuition/intuition.h>
  37. #include <graphics/display.h>
  38. #include <graphics/gfxbase.h>
  39. #include <graphics/gfx.h>
  40. #include <libraries/expansionbase.h>
  41.  
  42. void geta4(void);
  43. void Requestor_Handler(void);
  44. void Amiga_open(void);
  45. void Amiga_close(void);
  46. void open_requestor(void);
  47. void close_requestor(void);
  48. void write_byte(int x, int y, unsigned char n);
  49.  
  50. void write_cookie(unsigned char *brand, int line);
  51. void make_hame_palette(struct ViewPort *vp);
  52. void SetRGB8 (short reg, unsigned char rr, unsigned char gg,
  53.               unsigned char bb, short base);
  54.  
  55. int open_ham(int input_width, int inpupt_height);
  56. void  write_ham_pixel(UWORD x, UWORD y, UBYTE r, UBYTE g, UBYTE b);
  57. int open_hame(void);
  58. void  write_hame_pixel(int x, int y, char Red, char Green, char Blue);
  59. int open_firecracker(void);
  60. void  write_firecracker_pixel(UWORD x, UWORD y, UBYTE r, UBYTE g, UBYTE b);
  61.  
  62. extern unsigned int Options;
  63. extern char DisplayFormat;
  64. extern FRAME Frame;
  65.  
  66. #define INT_REV 29L
  67. #define GR_REV 29L
  68.  
  69. #define MAXDEPTHAGA    8
  70. #define MAXDEPTHNONAGA    6
  71.  
  72. struct IntuitionBase *IntuitionBase;
  73. struct GfxBase *GfxBase;
  74. struct Library *ExpansionBase;
  75.  
  76. struct Screen *s = NULL;
  77. volatile struct Window *w;
  78. struct Task *Requestor_Task;
  79.  
  80. volatile int Requestor_Running;
  81. volatile extern int Stop_Flag;
  82.  
  83. int width, height, depth;
  84. int bytesperrow;
  85. int leftedge, rightedge;
  86. int topedge, bottomedge;
  87.  
  88. int IsAGA;
  89.  
  90. struct Rectangle Rect1 =
  91.    {
  92.    0, 0, 0, 0
  93.    };
  94.  
  95. struct TagItem Ham_Screen_Tags[] =
  96.    {
  97.        { SA_DClip, &Rect1 },
  98.        { TAG_END, 0 }
  99.    };
  100.  
  101. struct ExtNewScreen Ham_Screen =
  102.    {
  103.    0, 0,
  104.    0, 0,
  105.    6,
  106.    0, 1,
  107.    HAM,
  108.    SCREENQUIET | NS_EXTENDED,
  109.    NULL,
  110.    (UBYTE *) "POV-Ray",
  111.    NULL,
  112.    NULL,
  113.    Ham_Screen_Tags
  114.    };
  115.  
  116.  
  117. struct NewScreen Ham_E_Screen =
  118.    {
  119.    0, 0,
  120.    0, 0,
  121.    4,
  122.    0, 1,
  123.    INTERLACE | HIRES,
  124.    SCREENQUIET,
  125.    NULL,
  126.    (UBYTE *) "POV-Ray",
  127.    NULL,
  128.    NULL
  129.    };
  130.  
  131. int lacer; /* if non-zero, screen is an interlace screen. Set this... */
  132.            /* ...as soon as you open your HAM-E screen.               */
  133.  
  134. unsigned char *fp0,*fp1,*fp2,*fp3; /* These are pointers which have been... */
  135.                                    /* ...cached from the screens BitMap[]   */
  136.                                    /* ...array. This allows us to get at    */
  137.                                    /* ...them much faster. Set them as soon */
  138.                                    /* ...as you open your HAM-E screen.     */
  139.  
  140. unsigned char bitpat[] =    /* This table is used as a table of masks... */
  141.   {                         /* ...to isolate bits in the HAM-E pixels    */
  142.     128,64,32,16,8,4,2,1,
  143.   };
  144.  
  145. unsigned char ham_cookie[] =  /* ham mode cookie... preceeds any HAM...    */
  146.   {                           /* ...color registers, and triggers hardware */
  147.     0xA2,0xF5,0x84,0xDC,      /* ...into ham mode.                         */
  148.     0x6D,0xB0,0x7F,0x18
  149.   };
  150.  
  151. struct Window *Requestor_Window;
  152. volatile struct MsgPort *Requestor_Port;
  153.  
  154. struct IntuiText chip Body_Text =
  155.    {0, 1, JAM1, 5, 10, NULL, (UBYTE *) "Click to abort the picture", NULL};
  156.  
  157. struct IntuiText chip Abort_Text =
  158.    {0, 1, JAM1, 5, 3, NULL, (UBYTE *) "Abort", NULL};
  159.  
  160. UWORD chip ColorTbl[16] = { 0x000, 0x111, 0x222, 0x333, 0x444, 0x555, 0x666,
  161.                        0x777, 0x888, 0x999, 0xaaa, 0xbbb, 0xccc, 0xddd,
  162.                        0xeee, 0xfff };
  163.  
  164. LONG last_red, last_green, last_blue, last_x, last_y;
  165.  
  166. /* Firecracker routines */
  167. typedef struct BOARD {
  168.     ULONG    orgb;
  169.     ULONG    orgb2;        /* autoincrementing */
  170.     UWORD    pad;
  171.     UBYTE    control0;
  172.     UBYTE    control1;
  173.     UWORD    y, x;
  174. } BOARD;
  175.  
  176. BOARD        *board;
  177.  
  178. void amiga_init_POVRAY(void )
  179.    {
  180.    (void) onbreak(amiga_close_all);
  181.    }
  182.  
  183. int matherr (x)
  184.    struct exception *x;
  185.    {
  186.    fprintf (stderr, "Math error type: %d from function %s values: %g %g\n",
  187.            x->type, x->name, x->arg1, x->arg2);
  188.  
  189.    switch(x->type) 
  190.      {
  191.      case DOMAIN:
  192.      case OVERFLOW:
  193.         x->retval = 1.0e17;
  194.         break;
  195.  
  196.      case SING:
  197.      case UNDERFLOW:
  198.         x->retval = 0.0;
  199.         break;
  200.  
  201.      case TLOSS:
  202.      case PLOSS:
  203.         return (0);
  204.  
  205.      default:
  206.         break;
  207.      }
  208.    return(1);
  209.    }
  210.  
  211. void Requestor_Handler ()
  212.    {
  213.    Requestor_Port = CreatePort ("ray trace port", 0L);
  214.    Requestor_Window = BuildSysRequest
  215.              (NULL, &Body_Text, NULL, &Abort_Text, GADGETUP, 280L, 60L);
  216.    Wait ((1 << Requestor_Port -> mp_SigBit)
  217.           | (1 << Requestor_Window -> UserPort -> mp_SigBit));
  218.  
  219.    Requestor_Running = FALSE;
  220.    Stop_Flag = TRUE;
  221.    }
  222.  
  223. void Amiga_open()
  224.    {
  225.    IntuitionBase = (struct IntuitionBase *) OpenLibrary ("intuition.library",INT_REV);
  226.    if (IntuitionBase == NULL)
  227.      exit(FALSE);
  228.  
  229.    GfxBase = (struct GfxBase *) OpenLibrary ("graphics.library", GR_REV);
  230.    if (GfxBase == NULL)
  231.      exit(FALSE);
  232.  
  233.    IsAGA = (GfxBase->ChipRevBits0) ? TRUE : FALSE;
  234.  
  235.    Requestor_Running = FALSE;
  236.    }
  237.  
  238. void Amiga_close()
  239.    {
  240.    if (Requestor_Running) {
  241.       Signal (Requestor_Task, 1 << Requestor_Port -> mp_SigBit);
  242.       Delay (2L);
  243.       }
  244.  
  245.    if (Requestor_Window)
  246.       FreeSysRequest (Requestor_Window);
  247.  
  248.    Requestor_Window = NULL;
  249.  
  250.    CloseLibrary ((struct Library *) GfxBase) ;
  251.    CloseLibrary (IntuitionBase) ;
  252.    }
  253.  
  254. void open_requestor()
  255.    {
  256.    Requestor_Window = NULL;
  257.    Stop_Flag = FALSE;
  258.    Requestor_Running = TRUE;
  259.    Requestor_Task = CreateTask ("Raytrace Requestor", 2L,
  260.                                 (APTR) Requestor_Handler, 20000L);
  261.    }
  262.  
  263. void display_finished ()
  264.    {
  265.    if (Requestor_Running) {
  266.      Signal (Requestor_Task, 1 << Requestor_Port -> mp_SigBit);
  267.      Delay (2L);
  268.      }
  269.  
  270.    if (Requestor_Window)
  271.       FreeSysRequest (Requestor_Window);
  272.  
  273.    Requestor_Window = NULL;
  274.    if (Options & PROMPTEXIT)
  275.       {
  276.       printf ("Finished.\nPress CR to quit.\n");
  277.       getchar();
  278.       }
  279.    }
  280.  
  281. int open_ham (input_width, input_height)
  282. int input_width;
  283. int input_height;
  284. {
  285.     int screen_width, screen_height;
  286.     int viewmodes;
  287.  
  288.  
  289. #ifdef DBG
  290.  printf ("open_ham -\n");
  291.  printf ("  input_width: %4d input_height: %4d\n", input_width, input_height);
  292. #endif
  293.     viewmodes = HAM;
  294.  
  295.     if (input_width < 1 || input_height < 1)
  296.     {
  297.         display_close ();
  298.         printf ("? Size must be no smaller than 1 x 1\n");
  299.         return TRUE;
  300.     }
  301.  
  302.     if (input_width > 736 || input_height > 482)
  303.     {
  304.         display_close ();
  305.         printf ("? Size must be no larger than 736 x 482\n");
  306.         return TRUE;
  307.     }
  308.  
  309.     screen_width = 320;
  310.     if (input_width > 368 && input_width <= 736)
  311.     {
  312.         screen_width = 640;
  313.         viewmodes |= HIRES;
  314.     }
  315.     else if (input_width > 736)    /* Add higher resolutions */
  316.     {
  317.         screen_width = 640;
  318.         viewmodes |= HIRES;
  319.     }
  320.  
  321.     width = (input_width>screen_width) ? input_width : screen_width;
  322.  
  323.     leftedge = 0;
  324.     rightedge = width;
  325.     if (input_width < screen_width)
  326.     {
  327.         leftedge  = (screen_width - input_width) / 2;
  328.         rightedge = leftedge + input_width;
  329.     }
  330.  
  331.     screen_height = 200;
  332.     if (input_height > 241 && input_height <= 482)
  333.     {
  334.         screen_height = 400;
  335.         viewmodes |= LACE;
  336.     }
  337.     else if (input_height > 482)    /* Add higher resolutions */
  338.     {
  339.         screen_height = 400;
  340.         viewmodes |= LACE;
  341.     }
  342.  
  343.     height = (input_height>screen_height) ? input_height : screen_height;
  344.  
  345.     topedge = 0;
  346.     bottomedge = height;
  347.     if (input_height < screen_height)
  348.     {
  349.         topedge    = (screen_height - input_height) / 2;
  350.         bottomedge = topedge + input_height;
  351.     }
  352. #ifdef DBG
  353.  printf ("  screen_width:%4d screen_height:%4d\n", screen_width, screen_height);
  354.  printf ("  width:       %4d height:       %4d\n", width, height);
  355.  printf ("  leftedge:    %4d rightedge:    %4d\n", leftedge, rightedge);
  356.  printf ("  topedge:     %4d bottomedge:   %4d\n", topedge, bottomedge);
  357. #endif
  358.     depth = (IsAGA) ? MAXDEPTHAGA : MAXDEPTHNONAGA;
  359.  
  360.     Rect1.MaxX = width-1;
  361.     Rect1.MaxY = height-1;
  362.     Ham_Screen.Width = width;
  363.     Ham_Screen.Height = height;
  364.     Ham_Screen.Depth = depth;
  365.     Ham_Screen.ViewModes |= viewmodes;
  366.  
  367.     if ((s = (struct Screen *) OpenScreen (&Ham_Screen)) == NULL)
  368.     {
  369.        display_close ();
  370.        printf ("? Can't open screen.\n");
  371.        return TRUE;
  372.     }
  373.  
  374.     /* What did we actually get? */
  375.     width = s->Width;
  376.     height = s->Height;
  377.     depth = s->BitMap.Depth;
  378.     bytesperrow = s->BitMap.BytesPerRow;
  379.  
  380. #ifdef DBG
  381.  printf ("  actual_width:%4d actual_height:%4d\n", width, height);
  382.  printf ("  actual_depth:%4d\n", depth);
  383.  printf ("  bytesperrow: %4d\n", bytesperrow);
  384. #endif
  385.     ShowTitle (s, FALSE);
  386.  
  387.     LoadRGB4 (&(s->ViewPort), ColorTbl, 16L);
  388.     SetAPen (&(s->RastPort), 0L);
  389.     RectFill (&(s -> RastPort), 0L, 0L, width-1, height-1);
  390.  
  391.     return FALSE;
  392. }
  393.  
  394. #define absdif(x,y) ((x > y) ? (x - y) : (y - x))
  395. #define max3(x,y,z) ((x>y)?((x>z)?1:3):((y>z)?2:3))
  396.  
  397. #define HAM6_MASK    0x000F
  398. #define HAM6_RED    0x20
  399. #define HAM6_GREEN    0x30
  400. #define HAM6_BLUE    0x10
  401.  
  402. #define HAM8_MASK    0x003F
  403. #define HAM8_RED    0x80        //  1000 0000
  404. #define HAM8_GREEN    0xC0        //  1100 0000
  405. #define HAM8_BLUE    0x40        //  0100 0000
  406.  
  407. void write_ham_pixel (x, y, Red, Green, Blue)
  408. UWORD x, y;
  409. UBYTE Red, Green, Blue;
  410. {
  411.    register unsigned long index;
  412.    register int i;
  413.    register char *addr;
  414.    register UBYTE colour, mask, colour_mask;
  415.    UBYTE delta_red, delta_green, delta_blue;
  416.  
  417.  
  418.    y += topedge;
  419.    x += leftedge;
  420.  
  421.    if (y < last_y)
  422.       return;
  423.  
  424.    if (last_y != y) {
  425.       last_y = y;
  426.       last_red = last_green = last_blue = 0;
  427.       }
  428.    else
  429.       if (x <= last_x)
  430.          return;
  431.  
  432.    last_x = x;
  433.  
  434.    if (IsAGA) {
  435.       Red   = (Red   >> 2) & HAM8_MASK;
  436.       Green = (Green >> 2) & HAM8_MASK;
  437.       Blue  = (Blue  >> 2) & HAM8_MASK;
  438.    }
  439.    else {
  440.       Red   = (Red   >> 4) & HAM6_MASK;
  441.       Green = (Green >> 4) & HAM6_MASK;
  442.       Blue  = (Blue  >> 4) & HAM6_MASK;
  443.    }
  444.  
  445.    delta_red   = absdif (Red, last_red);
  446.    delta_green = absdif (Green, last_green);
  447.    delta_blue  = absdif (Blue, last_blue);
  448.  
  449.    if (IsAGA) {
  450.       switch (max3(delta_red, delta_green, delta_blue)) {
  451.       case 1:
  452.          last_red = Red;
  453.          colour = HAM8_RED   | Red;
  454.          break;
  455.       case 2:
  456.          last_green = Green;
  457.          colour = HAM8_GREEN | Green;
  458.          break;
  459.       case 3:
  460.          last_blue = Blue;
  461.          colour = HAM8_BLUE  | Blue;
  462.          break;
  463.       }
  464.    }
  465.    else {
  466.       switch (max3(delta_red, delta_green, delta_blue)) {
  467.       case 1:
  468.          last_red = Red;
  469.          colour = HAM6_RED   | Red;
  470.          break;
  471.       case 2:
  472.          last_green = Green;
  473.          colour = HAM6_GREEN | Green;
  474.          break;
  475.       case 3:
  476.          last_blue = Blue;
  477.          colour = HAM6_BLUE  | Blue;
  478.          break;
  479.       }
  480.    }
  481.  
  482.    index = (bytesperrow * y) + (x >> 3);
  483.    mask = 0x80 >> (x & 7);
  484.  
  485.    colour_mask = 1;
  486.  
  487.    for (i = 0 ; i < ((IsAGA) ? MAXDEPTHAGA : MAXDEPTHNONAGA) ; i++) {
  488.       addr = &s->BitMap.Planes[i][index];
  489.       *addr &= ~mask;
  490.       *addr |= (colour&colour_mask) ? mask : 0x00;
  491.       colour_mask <<= 1;
  492.       }
  493.    }
  494.  
  495. int open_hame ()
  496. {
  497.     int screen_width, screen_height;
  498.  
  499.  
  500.     screen_width = GfxBase->NormalDisplayColumns>>1;
  501.     Ham_E_Screen.Width = screen_width<<1;
  502.     screen_height = GfxBase->NormalDisplayRows<<1;
  503.     Ham_E_Screen.Height = screen_height+2;
  504.  
  505.     if ((s = (struct Screen *) OpenScreen (&Ham_E_Screen)) == NULL)
  506.     {
  507.         display_close ();
  508.         return (TRUE);
  509.     }
  510.  
  511.     ShowTitle (s, FALSE);
  512.  
  513.     lacer = 1;
  514.  
  515.     fp0 = s->BitMap.Planes[0];
  516.     fp1 = s->BitMap.Planes[1];
  517.     fp2 = s->BitMap.Planes[2];
  518.     fp3 = s->BitMap.Planes[3];
  519.  
  520.     make_hame_palette(&s->ViewPort);
  521.  
  522.     SetAPen (&(s->RastPort), 0L);
  523.     RectFill (&(s -> RastPort), 0L, 0L, Ham_E_Screen.Width-1, 1);
  524.     SetAPen (&(s->RastPort), 1L);
  525.     RectFill (&(s -> RastPort), 0L, 2L, Ham_E_Screen.Width-1,
  526.                                         Ham_E_Screen.Height-1);
  527.     write_cookie(ham_cookie, 0);
  528.     SetRGB8 (0x11, 0x80, 0x80, 0x80, 0);
  529.  
  530.     return FALSE;
  531. }
  532.  
  533. void write_hame_pixel (x, y, Red, Green, Blue)
  534.    int x, y;
  535.    char Red, Green, Blue;
  536.    {
  537.    register unsigned char colour;
  538.    short delta_red, delta_green, delta_blue;
  539.  
  540.    if (y < last_y)
  541.       return;
  542.  
  543.    if ((x >= width) || (y >= height))
  544.       return;
  545.  
  546.    if (last_y != y) {
  547.       last_y = y;
  548.       last_red = last_green = last_blue = 0;
  549.       }
  550.    else
  551.       if (x <= last_x)
  552.          return;
  553.  
  554.    last_x = x;
  555.  
  556.    Red = (Red >> 2) & 0x3F;
  557.    Green = (Green >> 2) & 0x3F;
  558.    Blue = (Blue >> 2) & 0x3F;
  559.  
  560.    delta_red = absdif (Red, last_red);
  561.    delta_green = absdif (Green, last_green);
  562.    delta_blue = absdif (Blue, last_blue);
  563.  
  564.    switch (max3(delta_red, delta_green, delta_blue)) {
  565.       case 1:
  566.          last_red = Red;
  567.          colour = 0x80 + Red;
  568.          break;
  569.       case 2:
  570.          last_green = Green;
  571.          colour = 0xc0 + Green;
  572.          break;
  573.       case 3:
  574.          last_blue = Blue;
  575.          colour = 0x40 + Blue;
  576.          break;
  577.       }
  578.  
  579.    write_byte (x, y+2, colour);
  580.    }
  581.  
  582. int open_firecracker()
  583. {
  584.     struct ConfigDev    *dev;
  585.     ULONG            i;
  586.  
  587.     ExpansionBase = OpenLibrary("expansion.library", 0L);
  588.     if (!ExpansionBase) {
  589.         printf("No expansion library\n");
  590.         return TRUE;
  591.     }
  592.  
  593.     dev = FindConfigDev(NULL, 0x838, 0);
  594.     if (dev) {
  595.         board = (BOARD *)dev->cd_BoardAddr;
  596.     }
  597.     else {
  598.         printf("Can't find FireCracker\n");
  599.         return TRUE;
  600.     }
  601.  
  602.     CloseLibrary((struct Library *) ExpansionBase);
  603.         height = 482;
  604.  
  605.         if (Frame.Screen_Width > 768) {
  606.        board->control0 = 0xf8;
  607.            width = 1024;
  608.            }
  609.         else if (Frame.Screen_Width > 512) {
  610.        board->control0 = 0xb8;           
  611.            width = 768;
  612.            }
  613.         else if (Frame.Screen_Width > 384) {
  614.        board->control0 = 0x78;
  615.            width = 512;
  616.            }
  617.         else {
  618.        board->control0 = 0x38;
  619.            width = 384;
  620.            }
  621.  
  622.     board->control1 = 0x7f;
  623.     board->y = 0; board->x = 0;
  624.     for (i=0; i<width*482; i++) {
  625.            /* I write to the auto-increment and non-autoincrement registers
  626.               to make this work on 68000's as well as 68020's. */
  627.            board->orgb  = 0x40404040;
  628.            board->orgb2 = 0x40404040;
  629.            }
  630.  
  631.     return FALSE;
  632. }
  633.  
  634.  
  635. void write_firecracker_pixel(x,y, r,g,b)
  636. UWORD    x,y;
  637. UBYTE    r,g,b;
  638. {
  639.     ULONG        v1,v2,v3,v;
  640.         int             new_x, new_y;
  641.  
  642.     v1 = r; v2 = g; v3 = b;
  643.     v = ((v1<<16)&0xff0000) + ((v2<<8)&0xff00) + (v3&0xff);
  644.  
  645.     new_y = y+(482-Frame.Screen_Height)/2; new_x = x+(width-Frame.Screen_Width)/2;
  646.         if ((new_x >= 0) && (new_y >= 0)
  647.             && (new_x < width) && (new_y < 482)) {
  648.            board->y = new_y;
  649.            board->x = new_x;
  650.        board->orgb2 = v;
  651.            }
  652. }
  653.  
  654. /*:
  655. .n write_byte()
  656. .k low_level write_byte byte_write write_pixel
  657. .b
  658.  
  659. SYNOPSIS:   void write_byte(x,y,n);
  660.                 short x;
  661.                 int y;
  662.                 unsigned char n;
  663.  
  664. FUNCTION:   This function is similar to the Amiga's WritePixel routine.
  665.             is simply takes the incoming position and value and writes
  666.             them to the HAM-E screen appropriately.
  667.  
  668. INPUTS:     x - horizontal position on screen.
  669.             y - vertical position on screen.
  670.             n - value 0-255 to write in the pixel.
  671.  
  672. RESULTS:    None
  673.  
  674. BUGS:       None Known.
  675.  
  676. LIMITATIONS:Hard coded for screen width of 320 pixels (640 hi-res pixels)
  677.  
  678. SEE ALSO:   Global variables "fp3", "fp2", "fp1", "fp0", "bitpat[]"
  679.  
  680. :*/
  681.  
  682. void write_byte(x,y,n)
  683.   int x;
  684.   int y;
  685.   unsigned char n;
  686.   {
  687.   register int ypos,byte_offset;
  688.   register short bit_offset;
  689.     ypos = y * 80; /* index to correct scan line - note hard coded width! */
  690.     bit_offset = (x << 1) & 7; /* find base bit position */
  691.     byte_offset = (x >> 2);   /* find base byte offset */
  692.     if (n & 128) *(fp3 + ypos + byte_offset) |= bitpat[bit_offset];
  693.             else *(fp3 + ypos + byte_offset) &= ~bitpat[bit_offset];
  694.     if (n &  64) *(fp2 + ypos + byte_offset) |= bitpat[bit_offset];
  695.             else *(fp2 + ypos + byte_offset) &= ~bitpat[bit_offset];
  696.     if (n &  32) *(fp1 + ypos + byte_offset) |= bitpat[bit_offset];
  697.             else *(fp1 + ypos + byte_offset) &= ~bitpat[bit_offset];
  698.     if (n &  16) *(fp0 + ypos + byte_offset) |= bitpat[bit_offset];
  699.             else *(fp0 + ypos + byte_offset) &= ~bitpat[bit_offset];
  700.     bit_offset++; /* to next nybble */
  701.     if (bit_offset == 8) /* carry into next byte? */
  702.       {
  703.         bit_offset=0;
  704.         byte_offset++;
  705.       }
  706.     if (n & 8) *(fp3 + ypos + byte_offset) |= bitpat[bit_offset];
  707.           else *(fp3 + ypos + byte_offset) &= ~bitpat[bit_offset];
  708.     if (n & 4) *(fp2 + ypos + byte_offset) |= bitpat[bit_offset];
  709.           else *(fp2 + ypos + byte_offset) &= ~bitpat[bit_offset];
  710.     if (n & 2) *(fp1 + ypos + byte_offset) |= bitpat[bit_offset];
  711.           else *(fp1 + ypos + byte_offset) &= ~bitpat[bit_offset];
  712.     if (n & 1) *(fp0 + ypos + byte_offset) |= bitpat[bit_offset];
  713.           else *(fp0 + ypos + byte_offset) &= ~bitpat[bit_offset];
  714.   }
  715.  
  716. /*:
  717. .n write_cookie()
  718. .k cookie low_level setup configure
  719. .b
  720.  
  721. SYNOPSIS:   void write_cookie(brand,line);
  722.                 unsigned char *brand;
  723.                 int line;
  724.  
  725. FUNCTION:    This function writes the cookie on a particular line.
  726.              The variable "brand" is a pointer to an arrary of data
  727.              that contains the particular cookie for the mode you want.
  728.              These arrays are the global ones "ham_cookie[]" and
  729.              "reg_cookie[]".
  730.              
  731.              Call as:
  732.              
  733.                   write_cookie(ham_cookie,line);
  734.                               -or-
  735.                   write_cookie(reg_cookie,line);
  736.              
  737.              Note: If you have a four line palette, you need to call
  738.              this function for each successive line the palette exists
  739.              upon, for example:
  740.              
  741.                   write_cookie(reg_cookie,0);
  742.                   write_cookie(reg_cookie,1);
  743.                   write_cookie(reg_cookie,2);
  744.                   write_cookie(reg_cookie,3);
  745.                   
  746.                         -or-
  747.                   
  748.                   for (i=0; i<4; i++)
  749.                     {
  750.                       write_cookie(reg_cookie,i);
  751.                     }
  752.              
  753.              If the global variable "lacer" is set, this function will
  754.              write the cookie data on the appropriate lines in both
  755.              fields... sending four lines of cookie to the function
  756.              with the lines 0,1,2,3 will write cookies on line pairs
  757.              0-1, 2-3, 4-5, and 6-7.
  758.  
  759. INPUTS:     A pointer to the apropriate cookie and the line to put it on.
  760.  
  761. RESULTS:    None
  762.  
  763. BUGS:       None Known.
  764.  
  765. LIMITATIONS:None Known.
  766.  
  767. SEE ALSO:   The global variable "lacer" and the "ham_cookie[]"
  768.             and reg_cookie[] global arrays.
  769.  
  770. :*/
  771. void write_cookie(brand,line)
  772.   unsigned char *brand;
  773.   int line;
  774.   {
  775.   int i;
  776.     if (lacer) /* we need double the cookie data! */
  777.       {
  778.         for (i=0; i<8; i++)
  779.           {
  780.             write_byte(i,line*2,brand[i]);
  781.             write_byte(i,(line*2)+1,brand[i]);
  782.           }
  783.       }
  784.     else
  785.       {
  786.         for (i=0; i<8; i++)
  787.           {
  788.             write_byte(i,line,brand[i]);
  789.           }
  790.       }
  791.   }
  792.  
  793. /*:
  794. .n make_hame_palette()
  795. .k amiga_palette palette_amiga setup configure
  796. .b
  797.  
  798. SYNOPSIS:   void make_hame_palette(vp);
  799.                 struct ViewPort *vp;
  800.  
  801. FUNCTION:   Sets palette for Amiga side of hardware: The palette here is
  802.             designed to achieve two independant goals. First, and most
  803.             importantly, it creates a situation where the IRGB lines at
  804.             the Amiga's data port will exactly mirror the data in the
  805.             bitplanes of the screen as each pixel is emitted. 
  806.             Secondly, this palette makes the images visible, if not
  807.             sensible, on a non HAM-e equipped Amiga... Hopefully this
  808.             distinctive color palette will quickly cue the user that
  809.             they are missing something good. :^)
  810.             
  811.             IRGB to 12 bit correspondence:
  812.             
  813.             bit 8 - b3 of red
  814.             bit 4 - b3 of green
  815.             bit 2 - b3 of blue
  816.             bit 1 - b0 of blue
  817.  
  818. INPUTS:     A pointer to the ViewPort that "belongs" to this screen.
  819.  
  820. RESULTS:    None
  821.  
  822. BUGS:       None Known.
  823.  
  824. LIMITATIONS:None Known.
  825.  
  826. SEE ALSO:   
  827.  
  828. :*/
  829. void make_hame_palette(vp)
  830.   struct ViewPort *vp;
  831.   {
  832.   int rr,gg,bb,i;
  833.   int col;
  834.     col=0;
  835.     for (i=0; i<16; i++)
  836.       {
  837.         rr=0; gg=0; bb=0;
  838.         if (i & 8) rr  = 8; /* this builds the IRGB bit outputs...   */
  839.         if (i & 4) gg  = 8; /* ...these four bits are all that are   */
  840.         if (i & 2) bb  = 8; /* ...required to make the HAM-E run,    */
  841.         if (i & 1) bb |= 1; /* ...they xfer b0->b3 to the IRGB lines */
  842.         if (i != 0)     /* we don't mess with c0 - we leave it black */
  843.           {
  844.             rr += (col & 7); /* build strange colors in cregs 1-15...  */
  845.             col += 2;        /* ...these extra bits sent to the color  */
  846.             gg += (col & 7); /* ...registers make amiga palette very   */
  847.             col += 2;        /* ...interesting to look at if the HAM-E */
  848.             bb += (col & 6); /* ...is NOT attached. Otherwise useless. */
  849.             col += 2;        /* ...The code in this "if" is optional.  */
  850.           }
  851.         SetRGB4(vp,i,rr,gg,bb); /* this actually sets the Amiga color regs */
  852.       }
  853.   }
  854.  
  855. /*:
  856. .n SetRGB8()
  857. .k palette_hame hame_palette setup configure
  858. .b
  859.  
  860. SYNOPSIS:   void SetRGB8(reg,rr,gg,bb,base);
  861.                 int reg,rr,gg,bb,base;
  862.  
  863. FUNCTION:   This routine sets the color registers in the HAM-E hardware.
  864.             It can handle color registers located at any point on screen,
  865.             by setting the "base" variable to the starting scan line where
  866.             the color registers exist. Normally, the cookie and it's
  867.             associated color registers are located beginning at scan line 0.
  868.             If the screen is an interlace screen, this routine will set both
  869.             set of color registers identically; when in interlace, the
  870.             HAM-E maintains separate sets of color registers for the
  871.             odd and even interlace fields. Since this routine sets both
  872.             sets of color registers identically, you don't have to
  873.             worry about dealing with this feature.
  874.             
  875. INPUTS:     reg  - the color register number from 0 to 255 to be set
  876.             rr   - the red value from 0-255
  877.             gg   - the green value from 0-255
  878.             bb   - the blue value from 0-255
  879.             base - the starting scan line of the first coookie position
  880.                    (usually zero)
  881.  
  882. RESULTS:    None
  883.  
  884. BUGS:       None Known.
  885.  
  886. LIMITATIONS:None Known.
  887.  
  888. SEE ALSO:   The global variable "lacer"
  889.  
  890. :*/
  891. void SetRGB8(reg,rr,gg,bb,base)
  892.   short reg;
  893.   unsigned char rr,gg,bb;
  894.   short base;
  895.   {
  896.   short p_row,p_index;
  897.     p_row = (reg >> 6) + base;           /* palette row 0-3.  */
  898.     p_index = ((reg & 0x3f) * 3) + 8;    /* reg 0-63 in p_row */
  899.     if (lacer) /* then we need both fields! */
  900.       {
  901.         /* for the even field: */
  902.         write_byte(p_index,   p_row*2, rr);         /* put RED value     */
  903.         write_byte(p_index+1, p_row*2, gg);         /* put GREEN value   */
  904.         write_byte(p_index+2, p_row*2, bb);         /* put BLUE value    */
  905.         
  906.         /* here is the stuff for the ODD field: */
  907.         write_byte(p_index,   (p_row*2)+1, rr);     /* put RED value     */
  908.         write_byte(p_index+1, (p_row*2)+1, gg);     /* put GREEN value   */
  909.         write_byte(p_index+2, (p_row*2)+1, bb);     /* put BLUE value    */
  910.       }
  911.     else /* just write to one field */
  912.       {
  913.         write_byte(p_index,   p_row, rr);     /* put RED value     */
  914.         write_byte(p_index+1, p_row, gg);     /* put GREEN value   */
  915.         write_byte(p_index+2, p_row, bb);     /* put BLUE value    */
  916.       }
  917.   }
  918.  
  919. void display_init (input_width, input_height)
  920. int input_width, input_height;
  921. {
  922.     Amiga_open();
  923.     open_requestor();
  924.  
  925.     Delay (10);
  926.  
  927.     last_red = last_green = last_blue = 0;
  928.     last_x = last_y = -1;
  929.  
  930.     if (DisplayFormat == 'E') {
  931.         if (open_hame () == TRUE) {
  932.         display_close ();
  933.         exit (FALSE);
  934.         }
  935.     }
  936.     else if (DisplayFormat == 'F') {
  937.         if (open_firecracker () == TRUE) {
  938.         display_close ();
  939.         exit (999);
  940.         }
  941.     }
  942.     else {
  943.         if (DisplayFormat != '2')    /*  If not +d2, then force */
  944.         IsAGA = FALSE;        /*    AGA off              */
  945.  
  946.         if (IsAGA)
  947.         printf ("(Rendering in AGA)\n");
  948.  
  949.         if (open_ham (input_width, input_height) == TRUE) {
  950.         display_close ();
  951.         exit (FALSE);
  952.         }
  953.     }
  954. }
  955.  
  956. void display_close ()
  957.    {
  958.    if (Requestor_Running) {
  959.       Signal (Requestor_Task, 1 << Requestor_Port -> mp_SigBit);
  960.       Delay (2L);
  961.       }
  962.  
  963.    if (Requestor_Window)
  964.       FreeSysRequest (Requestor_Window);
  965.  
  966.    Requestor_Window = NULL;
  967.  
  968.    if (s != NULL) {
  969.       CloseScreen (s);
  970.       s = NULL;
  971.       }
  972.    }
  973.  
  974. void display_plot (x, y, Red, Green, Blue)
  975.    int x, y;
  976.    char Red, Green, Blue;
  977.    {
  978.    if (DisplayFormat == 'E')
  979.     return (write_hame_pixel (x, y, Red, Green, Blue));
  980.    else if (DisplayFormat == 'F')
  981.     return (write_firecracker_pixel((UWORD) x, (UWORD) y,
  982.         (UBYTE) Red, (UBYTE) Green, (UBYTE) Blue));
  983.    else
  984.     return (write_ham_pixel ((UWORD) x, (UWORD) y,
  985.         (UBYTE) Red, (UBYTE) Green, (UBYTE) Blue));
  986.    }
  987.  
  988. int amiga_close_all ()
  989.    {
  990.    close_all();
  991.    return (1);
  992.    }
  993.